<?php
class ClaycodeNaviMenu
{
	private $arr_naviInfo;
	private $s_currUrl;
	private $arr_cons;
	private $arr_options;
	
	public function __construct($arr_naviInfo, $arr_options = null)
	{
		$this->arr_cons = array(
			'defaultNavElem'=> 'li',
			'defaultNavContainerElem'=> 'ul'
		);
		
		$this->arr_options = $arr_options;
		$this->arr_naviInfo = $arr_naviInfo;
		$this->s_currUrl = $this->getCurrUrl();
	}
	
	public function getNaviHtml($s_parentContElem = null, $s_elem=null)
	{
		if($s_parentContElem == null){
			$s_parentContElem = $this->arr_cons['defaultNavContainerElem'];
		}
		
		if($s_elem == null) {
			$s_elem = $this->arr_cons['defaultNavElem'];
		}
		
		$s_navi = '<' . $s_parentContElem . '>';
		
		if(isset($this->arr_naviInfo)) {
			foreach ($this->arr_naviInfo as $arr_navItem)
			{
				if($arr_navItem['hidden'] != true){
					$s_navi.= $this->buildNaviElem($s_elem, $arr_navItem);
					$s_navi.= $this->buildNaviLink($arr_navItem);
					$s_navi.= '</' . $s_elem . '>';
				}
			}	
		}
		$s_navi .= '</' . $s_parentContElem . '>';
		return $s_navi;
	}
	
	private function buildNaviElem($s_elem, $arr_navItem)
	{
		$s_naviElem = '';
		
		if(isset($arr_navItem['navi_nav_elem_class']) && trim($arr_navItem['navi_nav_elem_class']) != '') {
			$s_naviElem = '<' . $s_elem . ' class="' . $arr_navItem['navi_nav_elem_class'] . '">';
		} else {
			$s_naviElem = '<' . $s_elem . '>';
		}
		
		return $s_naviElem;
	}
	
	private function buildNaviLink($arr_navItem)
	{
		$s_naviLink = '';
		
		if(isset($arr_navItem['navi_link_class']) && trim($arr_navItem['navi_link_class']) != '') {
			$s_naviLink = '<a href="'
						. $this->buildUrl($arr_navItem['controller_class_alias'])
						. '" class="'
						. $arr_navItem['navi_link_class']
						. '">'
						. $arr_navItem['app_display_name']
						. '</a>';
		} else {
			$s_naviLink = '<a href="'
						. $this->buildUrl($arr_navItem['controller_class_alias'])
						. '">'
						. $arr_navItem['app_display_name']
						. '</a>';
		}
		
		return $s_naviLink;
	}
	
	private function buildUrl($s_controllerAlias)
	{
		include C_FW_customPackage . 'configs/applications.php';
		$s_url = '#';
		if($this->arr_options != null || !isset($this->arr_options)) {
			//pretty urls are activated so only controller alias to be appended
			if($this->arr_options['pretty_urls'] == true) {
				//$s_url = $this->s_currUrl . $s_appRequestVarName . '/' . $s_controllerAlias;
				if($this->arr_options['is_sub_navi'] == true){
					$s_url = $this->s_currUrl . $s_appRequestVarName . '/' . $this->arr_options['parent_app_alias']  . '/' . $s_subAppRequestVarName . '/' . $s_controllerAlias;
				}else{
					$s_url = $this->s_currUrl . $s_appRequestVarName . '/' . $s_controllerAlias;
					//$s_url = $this->s_currUrl . $s_controllerAlias;
				}
			} else {
				//pretty urls are deactivated
				if($this->arr_options['is_sub_navi'] == true){
					//sub navigation
					//add parameters for method calls to the parent application controller
					$s_url	= $this->s_currUrl 
							. $s_rootFile 
							. '?' 
							. $s_appRequestVarName 
							. '=' 
							. $this->arr_options['parent_app_alias'] 
							. '&'
							. $s_subAppRequestVarName
							. '='
							. $s_controllerAlias;
				}else{
					//parent application main navigation
					//the $s_appRequestVarName = the $_GET[varname] in the routing class
					$s_url = $this->s_currUrl . $s_rootFile . '?' . $s_appRequestVarName . '=' . $s_controllerAlias;
				}
			}
		}
		
		return $s_url;
	}
	
	private function getCurrUrl() {
		// output: /myproject/index.php
	    $currentPath = $_SERVER['PHP_SELF']; 
	 
	    // output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index )
	    $pathInfo = pathinfo($_SERVER['PHP_SELF']); 
	 
	    // output: localhost
	    $hostName = $_SERVER['HTTP_HOST']; 
	 
	    // output: http://
	    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
	 
	    // return: http://localhost/myproject/
	    return $protocol . $hostName . $pathInfo['dirname'] . "/";
	}
	
	public function __destruct()
	{
		
	}
}